[[...path]].page.tsx 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  1. import React, { useEffect } from 'react';
  2. import EventEmitter from 'events';
  3. import {
  4. IDataWithMeta, IPageInfoForEntity, IPagePopulatedToShowRevision, isClient, isIPageInfoForEntity, isServer, IUser, IUserHasId, pagePathUtils, pathUtils,
  5. } from '@growi/core';
  6. import ExtensibleCustomError from 'extensible-custom-error';
  7. import {
  8. NextPage, GetServerSideProps, GetServerSidePropsContext,
  9. } from 'next';
  10. import { serverSideTranslations } from 'next-i18next/serverSideTranslations';
  11. import dynamic from 'next/dynamic';
  12. import Head from 'next/head';
  13. import { useRouter } from 'next/router';
  14. import superjson from 'superjson';
  15. import { PageAlerts } from '~/components/PageAlert/PageAlerts';
  16. // import { PageComments } from '~/components/PageComment/PageComments';
  17. // import { useTranslation } from '~/i18n';
  18. import { CrowiRequest } from '~/interfaces/crowi-request';
  19. // import { renderScriptTagByName, renderHighlightJsStyleTag } from '~/service/cdn-resources-loader';
  20. // import { useIndentSize } from '~/stores/editor';
  21. // import { useRendererSettings } from '~/stores/renderer';
  22. // import { EditorMode, useEditorMode, useIsMobile } from '~/stores/ui';
  23. import { CustomWindow } from '~/interfaces/global';
  24. import { RendererConfig } from '~/interfaces/services/renderer';
  25. import { ISidebarConfig } from '~/interfaces/sidebar-config';
  26. import { IUserUISettings } from '~/interfaces/user-ui-settings';
  27. import { PageModel, PageDocument } from '~/server/models/page';
  28. import UserUISettings from '~/server/models/user-ui-settings';
  29. import Xss from '~/services/xss';
  30. import { useSWRxCurrentPage, useSWRxPageInfo } from '~/stores/page';
  31. import {
  32. usePreferDrawerModeByUser, usePreferDrawerModeOnEditByUser, useSidebarCollapsed, useCurrentSidebarContents, useCurrentProductNavWidth,
  33. } from '~/stores/ui';
  34. import loggerFactory from '~/utils/logger';
  35. // import { isUserPage, isTrashPage, isSharedPage } from '~/utils/path-utils';
  36. // import GrowiSubNavigation from '../client/js/components/Navbar/GrowiSubNavigation';
  37. // import GrowiSubNavigationSwitcher from '../client/js/components/Navbar/GrowiSubNavigationSwitcher';
  38. import { BasicLayout } from '../components/Layout/BasicLayout';
  39. import GrowiContextualSubNavigation from '../components/Navbar/GrowiContextualSubNavigation';
  40. import DisplaySwitcher from '../components/Page/DisplaySwitcher';
  41. // import { serializeUserSecurely } from '../server/models/serializers/user-serializer';
  42. // import PageStatusAlert from '../client/js/components/PageStatusAlert';
  43. import {
  44. useCurrentUser, useCurrentPagePath,
  45. useIsLatestRevision,
  46. useIsForbidden, useIsNotFound, useIsTrashPage, useIsSharedUser,
  47. useIsEnabledStaleNotification, useIsIdenticalPath,
  48. useIsSearchServiceConfigured, useIsSearchServiceReachable, useDisableLinkSharing,
  49. useHackmdUri,
  50. useIsAclEnabled, useIsUserPage, useIsNotCreatable,
  51. useCsrfToken, useIsSearchScopeChildrenAsDefault, useCurrentPageId, useCurrentPathname,
  52. useIsSlackConfigured, useIsBlinkedHeaderAtBoot, useRendererConfig, useEditingMarkdown,
  53. } from '../stores/context';
  54. import { useXss } from '../stores/xss';
  55. import {
  56. CommonProps, getNextI18NextConfig, getServerSideCommonProps, useCustomTitle,
  57. } from './utils/commons';
  58. import { registerTransformerForObjectId } from './utils/objectid-transformer';
  59. // import { useCurrentPageSWR } from '../stores/page';
  60. const logger = loggerFactory('growi:pages:all');
  61. const {
  62. isPermalink: _isPermalink, isUsersHomePage, isTrashPage: _isTrashPage, isUserPage, isCreatablePage,
  63. } = pagePathUtils;
  64. const { removeHeadingSlash } = pathUtils;
  65. type IPageToShowRevisionWithMeta = IDataWithMeta<IPagePopulatedToShowRevision & PageDocument, IPageInfoForEntity>;
  66. type IPageToShowRevisionWithMetaSerialized = IDataWithMeta<string, string>;
  67. // register custom serializer
  68. registerTransformerForObjectId();
  69. superjson.registerCustom<IPageToShowRevisionWithMeta, IPageToShowRevisionWithMetaSerialized>(
  70. {
  71. isApplicable: (v): v is IPageToShowRevisionWithMeta => {
  72. return v?.data != null
  73. && v?.data.toObject != null
  74. && v?.meta != null
  75. && isIPageInfoForEntity(v.meta);
  76. },
  77. serialize: (v) => {
  78. return {
  79. data: superjson.stringify(v.data.toObject()),
  80. meta: superjson.stringify(v.meta),
  81. };
  82. },
  83. deserialize: (v) => {
  84. return {
  85. data: superjson.parse(v.data),
  86. meta: v.meta != null ? superjson.parse(v.meta) : undefined,
  87. };
  88. },
  89. },
  90. 'IPageToShowRevisionWithMetaTransformer',
  91. );
  92. const IdenticalPathPage = (): JSX.Element => {
  93. const IdenticalPathPage = dynamic(() => import('../components/IdenticalPathPage').then(mod => mod.IdenticalPathPage), { ssr: false });
  94. return <IdenticalPathPage />;
  95. };
  96. const PutbackPageModal = (): JSX.Element => {
  97. const PutbackPageModal = dynamic(() => import('../components/PutbackPageModal'), { ssr: false });
  98. return <PutbackPageModal />;
  99. };
  100. type Props = CommonProps & {
  101. currentUser: IUser,
  102. pageWithMeta: IPageToShowRevisionWithMeta,
  103. // pageUser?: any,
  104. // redirectTo?: string;
  105. // redirectFrom?: string;
  106. // shareLinkId?: string;
  107. isLatestRevision?: boolean
  108. isIdenticalPathPage?: boolean,
  109. isForbidden: boolean,
  110. isNotFound: boolean,
  111. IsNotCreatable: boolean,
  112. // isAbleToDeleteCompletely: boolean,
  113. isSearchServiceConfigured: boolean,
  114. isSearchServiceReachable: boolean,
  115. isSearchScopeChildrenAsDefault: boolean,
  116. isSlackConfigured: boolean,
  117. // isMailerSetup: boolean,
  118. isAclEnabled: boolean,
  119. // hasSlackConfig: boolean,
  120. // drawioUri: string,
  121. hackmdUri: string,
  122. // mathJax: string,
  123. // noCdn: string,
  124. // highlightJsStyle: string,
  125. // isAllReplyShown: boolean,
  126. // isContainerFluid: boolean,
  127. // editorConfig: any,
  128. isEnabledStaleNotification: boolean,
  129. // isEnabledLinebreaks: boolean,
  130. // isEnabledLinebreaksInComments: boolean,
  131. // adminPreferredIndentSize: number,
  132. // isIndentSizeForced: boolean,
  133. disableLinkSharing: boolean,
  134. rendererConfig: RendererConfig,
  135. // UI
  136. userUISettings?: IUserUISettings
  137. // Sidebar
  138. sidebarConfig: ISidebarConfig,
  139. };
  140. const GrowiPage: NextPage<Props> = (props: Props) => {
  141. // const { t } = useTranslation();
  142. const router = useRouter();
  143. const UnsavedAlertDialog = dynamic(() => import('./UnsavedAlertDialog'), { ssr: false });
  144. const GrowiSubNavigationSwitcher = dynamic(() => import('../components/Navbar/GrowiSubNavigationSwitcher'), { ssr: false });
  145. const { data: currentUser } = useCurrentUser(props.currentUser ?? null);
  146. // register global EventEmitter
  147. if (isClient()) {
  148. (window as CustomWindow).globalEmitter = new EventEmitter();
  149. }
  150. // commons
  151. useXss(new Xss());
  152. // useEditorConfig(props.editorConfig);
  153. useCsrfToken(props.csrfToken);
  154. // UserUISettings
  155. usePreferDrawerModeByUser(props.userUISettings?.preferDrawerModeByUser ?? props.sidebarConfig.isSidebarDrawerMode);
  156. usePreferDrawerModeOnEditByUser(props.userUISettings?.preferDrawerModeOnEditByUser);
  157. useSidebarCollapsed(props.userUISettings?.isSidebarCollapsed ?? props.sidebarConfig.isSidebarClosedAtDockMode);
  158. useCurrentSidebarContents(props.userUISettings?.currentSidebarContents);
  159. useCurrentProductNavWidth(props.userUISettings?.currentProductNavWidth);
  160. // page
  161. useCurrentPagePath(props.currentPathname);
  162. useIsLatestRevision(props.isLatestRevision);
  163. // useOwnerOfCurrentPage(props.pageUser != null ? JSON.parse(props.pageUser) : null);
  164. useIsForbidden(props.isForbidden);
  165. useIsNotFound(props.isNotFound);
  166. useIsNotCreatable(props.IsNotCreatable);
  167. // useIsTrashPage(_isTrashPage(props.currentPagePath));
  168. // useShared();
  169. // useShareLinkId(props.shareLinkId);
  170. useIsSharedUser(false); // this page cann't be routed for '/share'
  171. useIsIdenticalPath(false); // TODO: need to initialize from props
  172. // useIsAbleToDeleteCompletely(props.isAbleToDeleteCompletely);
  173. useIsEnabledStaleNotification(props.isEnabledStaleNotification);
  174. useIsBlinkedHeaderAtBoot(false);
  175. useIsSearchServiceConfigured(props.isSearchServiceConfigured);
  176. useIsSearchServiceReachable(props.isSearchServiceReachable);
  177. useIsSearchScopeChildrenAsDefault(props.isSearchScopeChildrenAsDefault);
  178. useIsSlackConfigured(props.isSlackConfigured);
  179. // useIsMailerSetup(props.isMailerSetup);
  180. useIsAclEnabled(props.isAclEnabled);
  181. // useHasSlackConfig(props.hasSlackConfig);
  182. // useDrawioUri(props.drawioUri);
  183. useHackmdUri(props.hackmdUri);
  184. // useMathJax(props.mathJax);
  185. // useNoCdn(props.noCdn);
  186. // useIndentSize(props.adminPreferredIndentSize);
  187. useDisableLinkSharing(props.disableLinkSharing);
  188. useRendererConfig(props.rendererConfig);
  189. // useRendererSettings(props.rendererSettingsStr != null ? JSON.parse(props.rendererSettingsStr) : undefined);
  190. // useGrowiRendererConfig(props.growiRendererConfigStr != null ? JSON.parse(props.growiRendererConfigStr) : undefined);
  191. // const { data: editorMode } = useEditorMode();
  192. const { pageWithMeta, userUISettings } = props;
  193. let shouldRenderPutbackPageModal = false;
  194. if (pageWithMeta != null) {
  195. shouldRenderPutbackPageModal = _isTrashPage(pageWithMeta.data.path);
  196. }
  197. useCurrentPageId(pageWithMeta?.data._id);
  198. useSWRxCurrentPage(undefined, pageWithMeta?.data); // store initial data
  199. // useSWRxPage(pageWithMeta?.data._id);
  200. useSWRxPageInfo(pageWithMeta?.data._id, undefined, pageWithMeta?.meta); // store initial data
  201. useIsTrashPage(_isTrashPage(pageWithMeta?.data.path ?? ''));
  202. useIsUserPage(isUserPage(pageWithMeta?.data.path ?? ''));
  203. useIsNotCreatable(props.isForbidden || !isCreatablePage(pageWithMeta?.data.path ?? '')); // TODO: need to include props.isIdentical
  204. useCurrentPagePath(pageWithMeta?.data.path);
  205. useCurrentPathname(props.currentPathname);
  206. useEditingMarkdown(pageWithMeta?.data.revision.body);
  207. // sync pathname by Shallow Routing https://nextjs.org/docs/routing/shallow-routing
  208. useEffect(() => {
  209. if (isClient() && window.location.pathname !== props.currentPathname) {
  210. router.replace(props.currentPathname, undefined, { shallow: true });
  211. }
  212. }, [props.currentPathname, router]);
  213. const classNames: string[] = [];
  214. // switch (editorMode) {
  215. // case EditorMode.Editor:
  216. // classNames.push('on-edit', 'builtin-editor');
  217. // break;
  218. // case EditorMode.HackMD:
  219. // classNames.push('on-edit', 'hackmd');
  220. // break;
  221. // }
  222. // if (props.isContainerFluid) {
  223. // classNames.push('growi-layout-fluid');
  224. // }
  225. // if (page == null) {
  226. // classNames.push('not-found-page');
  227. // }
  228. return (
  229. <>
  230. <Head>
  231. {/*
  232. {renderScriptTagByName('drawio-viewer')}
  233. {renderScriptTagByName('mathjax')}
  234. {renderScriptTagByName('highlight-addons')}
  235. {renderHighlightJsStyleTag(props.highlightJsStyle)}
  236. */}
  237. </Head>
  238. {/* <BasicLayout title={useCustomTitle(props, t('GROWI'))} className={classNames.join(' ')}> */}
  239. <BasicLayout title={useCustomTitle(props, 'GROWI')} className={classNames.join(' ')}>
  240. <header className="py-0">
  241. <GrowiContextualSubNavigation isLinkSharingDisabled={props.disableLinkSharing} />
  242. </header>
  243. <div className="d-edit-none">
  244. <GrowiSubNavigationSwitcher />
  245. </div>
  246. <div id="grw-subnav-sticky-trigger" className="sticky-top"></div>
  247. <div id="grw-fav-sticky-trigger" className="sticky-top"></div>
  248. <div id="main" className={`main ${isUsersHomePage(props.currentPathname) && 'user-page'}`}>
  249. <div id="content-main" className="content-main grw-container-convertible">
  250. <div className="row">
  251. <div className="col">
  252. { props.isIdenticalPathPage && <IdenticalPathPage /> }
  253. { !props.isIdenticalPathPage && (
  254. <>
  255. <PageAlerts />
  256. { props.isForbidden
  257. ? <>ForbiddenPage</>
  258. : <DisplaySwitcher />
  259. }
  260. <div id="page-editor-navbar-bottom-container" className="d-none d-edit-block"></div>
  261. {/* <PageStatusAlert /> */}
  262. PageStatusAlert
  263. </>
  264. ) }
  265. </div>
  266. </div>
  267. {/* <div className="col-xl-2 col-lg-3 d-none d-lg-block revision-toc-container">
  268. <div id="revision-toc" className="revision-toc mt-3 sps sps--abv" data-sps-offset="123">
  269. <div id="revision-toc-content" className="revision-toc-content"></div>
  270. </div>
  271. </div> */}
  272. </div>
  273. </div>
  274. <footer>
  275. {/* <PageComments /> */}
  276. PageComments
  277. </footer>
  278. <UnsavedAlertDialog />
  279. {shouldRenderPutbackPageModal && <PutbackPageModal />}
  280. </BasicLayout>
  281. </>
  282. );
  283. };
  284. function getPageIdFromPathname(currentPathname: string): string | null {
  285. return _isPermalink(currentPathname) ? removeHeadingSlash(currentPathname) : null;
  286. }
  287. class MultiplePagesHitsError extends ExtensibleCustomError {
  288. pagePath: string;
  289. constructor(pagePath: string) {
  290. super(`MultiplePagesHitsError occured by '${pagePath}'`);
  291. this.pagePath = pagePath;
  292. }
  293. }
  294. async function injectPageData(context: GetServerSidePropsContext, props: Props): Promise<void> {
  295. const req: CrowiRequest = context.req as CrowiRequest;
  296. const { crowi } = req;
  297. const { revisionId } = req.query;
  298. const Page = crowi.model('Page') as PageModel;
  299. const { pageService } = crowi;
  300. const { currentPathname } = props;
  301. const pageId = getPageIdFromPathname(currentPathname);
  302. const isPermalink = _isPermalink(currentPathname);
  303. const { user } = req;
  304. // check whether the specified page path hits to multiple pages
  305. if (!isPermalink) {
  306. const count = await Page.countByPathAndViewer(currentPathname, user, null, true);
  307. if (count > 1) {
  308. throw new MultiplePagesHitsError(currentPathname);
  309. }
  310. }
  311. const pageWithMeta: IPageToShowRevisionWithMeta = await pageService.findPageAndMetaDataByViewer(pageId, currentPathname, user, true); // includeEmpty = true, isSharedPage = false
  312. const page = pageWithMeta?.data as unknown as PageDocument;
  313. // populate & check if the revision is latest
  314. if (page != null) {
  315. page.initLatestRevisionField(revisionId);
  316. await page.populateDataToShowRevision();
  317. props.isLatestRevision = page.isLatestRevision();
  318. }
  319. props.pageWithMeta = pageWithMeta;
  320. }
  321. async function injectUserUISettings(context: GetServerSidePropsContext, props: Props): Promise<void> {
  322. const req = context.req as CrowiRequest<IUserHasId & any>;
  323. const { user } = req;
  324. const userUISettings = user == null ? null : await UserUISettings.findOne({ user: user._id }).exec();
  325. if (userUISettings != null) {
  326. props.userUISettings = userUISettings.toObject();
  327. }
  328. }
  329. async function injectRoutingInformation(context: GetServerSidePropsContext, props: Props): Promise<void> {
  330. const req: CrowiRequest = context.req as CrowiRequest;
  331. const { crowi } = req;
  332. const Page = crowi.model('Page') as PageModel;
  333. const { currentPathname } = props;
  334. const pageId = getPageIdFromPathname(currentPathname);
  335. const isPermalink = _isPermalink(currentPathname);
  336. const page = props.pageWithMeta?.data;
  337. if (props.isIdenticalPathPage) {
  338. // TBD
  339. }
  340. else if (page == null) {
  341. props.isNotFound = true;
  342. props.IsNotCreatable = !isCreatablePage(currentPathname);
  343. // check the page is forbidden or just does not exist.
  344. const count = isPermalink ? await Page.count({ _id: pageId }) : await Page.count({ path: currentPathname });
  345. props.isForbidden = count > 0;
  346. }
  347. else {
  348. // /62a88db47fed8b2d94f30000 ==> /path/to/page
  349. if (isPermalink && page.isEmpty) {
  350. props.currentPathname = page.path;
  351. }
  352. // /path/to/page ==> /62a88db47fed8b2d94f30000
  353. if (!isPermalink && !page.isEmpty) {
  354. const isToppage = pagePathUtils.isTopPage(props.currentPathname);
  355. if (!isToppage) {
  356. props.currentPathname = `/${page._id}`;
  357. }
  358. }
  359. }
  360. }
  361. // async function injectPageUserInformation(context: GetServerSidePropsContext, props: Props): Promise<void> {
  362. // const req: CrowiRequest = context.req as CrowiRequest;
  363. // const { crowi } = req;
  364. // const UserModel = crowi.model('User');
  365. // if (isUserPage(props.currentPagePath)) {
  366. // const user = await UserModel.findUserByUsername(UserModel.getUsernameByPath(props.currentPagePath));
  367. // if (user != null) {
  368. // props.pageUser = JSON.stringify(user.toObject());
  369. // }
  370. // }
  371. // }
  372. function injectServerConfigurations(context: GetServerSidePropsContext, props: Props): void {
  373. const req: CrowiRequest = context.req as CrowiRequest;
  374. const { crowi } = req;
  375. const {
  376. appService, searchService, configManager, aclService, slackNotificationService, mailService,
  377. } = crowi;
  378. props.isSearchServiceConfigured = searchService.isConfigured;
  379. props.isSearchServiceReachable = searchService.isReachable;
  380. props.isSearchScopeChildrenAsDefault = configManager.getConfig('crowi', 'customize:isSearchScopeChildrenAsDefault');
  381. props.isSlackConfigured = crowi.slackIntegrationService.isSlackConfigured;
  382. // props.isMailerSetup = mailService.isMailerSetup;
  383. props.isAclEnabled = aclService.isAclEnabled();
  384. // props.hasSlackConfig = slackNotificationService.hasSlackConfig();
  385. // props.drawioUri = configManager.getConfig('crowi', 'app:drawioUri');
  386. props.hackmdUri = configManager.getConfig('crowi', 'app:hackmdUri');
  387. // props.mathJax = configManager.getConfig('crowi', 'app:mathJax');
  388. // props.noCdn = configManager.getConfig('crowi', 'app:noCdn');
  389. // props.highlightJsStyle = configManager.getConfig('crowi', 'customize:highlightJsStyle');
  390. // props.isAllReplyShown = configManager.getConfig('crowi', 'customize:isAllReplyShown');
  391. // props.isContainerFluid = configManager.getConfig('crowi', 'customize:isContainerFluid');
  392. props.isEnabledStaleNotification = configManager.getConfig('crowi', 'customize:isEnabledStaleNotification');
  393. // props.isEnabledLinebreaks = configManager.getConfig('markdown', 'markdown:isEnabledLinebreaks');
  394. // props.isEnabledLinebreaksInComments = configManager.getConfig('markdown', 'markdown:isEnabledLinebreaksInComments');
  395. props.disableLinkSharing = configManager.getConfig('crowi', 'security:disableLinkSharing');
  396. // props.editorConfig = {
  397. // upload: {
  398. // image: crowi.fileUploadService.getIsUploadable(),
  399. // file: crowi.fileUploadService.getFileUploadEnabled(),
  400. // },
  401. // };
  402. // props.adminPreferredIndentSize = configManager.getConfig('markdown', 'markdown:adminPreferredIndentSize');
  403. // props.isIndentSizeForced = configManager.getConfig('markdown', 'markdown:isIndentSizeForced');
  404. props.rendererConfig = {
  405. isEnabledLinebreaks: configManager.getConfig('markdown', 'markdown:isEnabledLinebreaks'),
  406. isEnabledLinebreaksInComments: configManager.getConfig('markdown', 'markdown:isEnabledLinebreaksInComments'),
  407. adminPreferredIndentSize: configManager.getConfig('markdown', 'markdown:adminPreferredIndentSize'),
  408. isIndentSizeForced: configManager.getConfig('markdown', 'markdown:isIndentSizeForced'),
  409. plantumlUri: process.env.PLANTUML_URI ?? null,
  410. blockdiagUri: process.env.BLOCKDIAG_URI ?? null,
  411. // XSS Options
  412. isEnabledXssPrevention: configManager.getConfig('markdown', 'markdown:xss:isEnabledPrevention'),
  413. attrWhiteList: crowi.xssService.getAttrWhiteList(),
  414. tagWhiteList: crowi.xssService.getTagWhiteList(),
  415. highlightJsStyleBorder: crowi.configManager.getConfig('crowi', 'customize:highlightJsStyleBorder'),
  416. };
  417. props.sidebarConfig = {
  418. isSidebarDrawerMode: configManager.getConfig('crowi', 'customize:isSidebarDrawerMode'),
  419. isSidebarClosedAtDockMode: configManager.getConfig('crowi', 'customize:isSidebarClosedAtDockMode'),
  420. };
  421. }
  422. /**
  423. * for Server Side Translations
  424. * @param context
  425. * @param props
  426. * @param namespacesRequired
  427. */
  428. async function injectNextI18NextConfigurations(context: GetServerSidePropsContext, props: Props, namespacesRequired?: string[] | undefined): Promise<void> {
  429. const nextI18NextConfig = await getNextI18NextConfig(serverSideTranslations, context, namespacesRequired);
  430. props._nextI18Next = nextI18NextConfig._nextI18Next;
  431. }
  432. export const getServerSideProps: GetServerSideProps = async(context: GetServerSidePropsContext) => {
  433. const req = context.req as CrowiRequest<IUserHasId & any>;
  434. const { user } = req;
  435. const result = await getServerSideCommonProps(context);
  436. // check for presence
  437. // see: https://github.com/vercel/next.js/issues/19271#issuecomment-730006862
  438. if (!('props' in result)) {
  439. throw new Error('invalid getSSP result');
  440. }
  441. const props: Props = result.props as Props;
  442. if (user != null) {
  443. props.currentUser = user.toObject();
  444. }
  445. try {
  446. await injectPageData(context, props);
  447. }
  448. catch (err) {
  449. if (err instanceof MultiplePagesHitsError) {
  450. props.isIdenticalPathPage = true;
  451. }
  452. else {
  453. throw err;
  454. }
  455. }
  456. await injectUserUISettings(context, props);
  457. await injectRoutingInformation(context, props);
  458. injectServerConfigurations(context, props);
  459. await injectNextI18NextConfigurations(context, props, ['translation']);
  460. return {
  461. props,
  462. };
  463. };
  464. export default GrowiPage;